<?php if(! defined('HAS_POWER') ) exit('无权访问!');
/**
==============================================================================
* Class AgentComponent GracePHP用户代理类组件
* @author 肖永标
* @date 2014-12-12
==============================================================================
*/

class AgentComponent extends Component {

    /** 返回当前操作系统
     * @return string
     */
    public function os()
    {
        if(!empty($_SERVER['HTTP_USER_AGENT']))
        {
            $os = $_SERVER['HTTP_USER_AGENT'];
            if (preg_match('/win/i',$os))
                $os = 'Windows';
            elseif (preg_match('/mac/i',$os))
                $os = 'MAC';
            elseif (preg_match('/linux/i',$os))
                $os = 'Linux';
            elseif (preg_match('/unix/i',$os))
                $os = 'Unix';
            elseif (preg_match('/bsd/i',$os))
                $os = 'BSD';
            else
                $os = 'Other';
            return $os;
        }
        else
            return "unknown";
    }


    /** 返回用户浏览器
     * @return string
     */
    public function browser()
    {
        if(strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 8.0'))
            return 'Internet Explorer 8.0';
        else if(strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 7.0'))
            return 'Internet Explorer 7.0';
        else if(strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 6.0'))
            return 'Internet Explorer 6.0';
        else if(strpos($_SERVER['HTTP_USER_AGENT'],'Firefox/3'))
            return 'Firefox 3';
        else if(strpos($_SERVER['HTTP_USER_AGENT'],'Firefox/2'))
            return 'Firefox 2';
        else if(strpos($_SERVER['HTTP_USER_AGENT'],'Chrome'))
            return 'Google Chrome';
        else if(strpos($_SERVER['HTTP_USER_AGENT'],'Safari'))
            return 'Safari';
        else if(strpos($_SERVER['HTTP_USER_AGENT'],'Opera'))
            return 'Opera';
        else
            return $_SERVER['HTTP_USER_AGENT'];
    }


    /** 判断是否移动设备
     * @return bool
     */
    public function isMobile()
    {
        if(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone)/i',
            strtolower($_SERVER['HTTP_USER_AGENT'])))
        {
            return true;
        }

        if((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml')>0) or
            ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE']))))
        {
            return true;
        }

        $mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4));
        $mobile_agents = array(
                        'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
                        'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
                        'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
                        'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
                        'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
                        'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
                        'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
                        'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
                        'wapr','webc','winw','winw','xda','xda-');

        if(in_array($mobile_ua,$mobile_agents))
            return true;
            
        if (strpos(strtolower($_SERVER['ALL_HTTP']),'OperaMini')>0) 
            return true;

        return false;
    }


    /** 判断是否爬虫机器人
     * @return bool
     */
    public function isRobot()
    {
    	if (empty($_SERVER['HTTP_USER_AGENT']))
    		return false;
	
    	$searchEngineBot = array(
    		'googlebot',
    		'mediapartners-google',
    		'baiduspider',
    		'msnbot',
    		'yodaobot',
    		'youdaobot',
    		'yahoo! slurp',
    		'yahoo! slurp china',
    		'iaskspider',
    		'sogou web spider',
    		'sogou push spider',
    		'sosospider',
    		'spider',
    		'crawler'
    	);
	
    	$spider = strtolower($_SERVER['HTTP_USER_AGENT']);
        
        if(Config::getItem('robots'))
            $searchEngineBot = array_merge($searchEngineBot, Config::getItem('robots'));
        
    	foreach ($searchEngineBot as $key => $value)
    	{  
    		if (strpos($spider, $value) !== false)
    			return true;
    	}
    	
    	return false;
    }


    /** 判断是否是从另一个网站进入的
     * @return bool
     */
    public function isReferral()
    {
        $host = explode('/', $_SERVER['HTTP_REFERER']);
        if($host == $_SERVER['HTTP_HOST'])
            return true;
        return false;
    }


    /** 是否允许被加载
     * @return bool
     */
    public function allowLoad()
    {
        return true;
    }
}